//创建加载状态相关的小仓库
import { defineStore } from 'pinia';

//定义加载状态的接口
interface LoadingState {
    globalLoading: boolean;
    loadingText: string;
    loadingCount: number;
}

//创建加载状态小仓库
let useLoadingStore = defineStore('Loading', {
    //小仓库存储数据地方
    state: (): LoadingState => {
        return {
            globalLoading: false,
            loadingText: '加载中...',
            loadingCount: 0
        }
    },
    //异步|逻辑的地方
    actions: {
        //显示全局加载
        showLoading(text: string = '加载中...') {
            this.loadingText = text;
            this.loadingCount++;
            this.globalLoading = true;
        },
        //隐藏全局加载
        hideLoading() {
            this.loadingCount--;
            if (this.loadingCount <= 0) {
                this.globalLoading = false;
                this.loadingCount = 0;
            }
        },
        //重置加载状态
        resetLoading() {
            this.globalLoading = false;
            this.loadingCount = 0;
            this.loadingText = '加载中...';
        }
    },
    getters: {
        //是否正在加载
        isLoading: (state) => state.globalLoading
    }
})

//对外暴露获取小仓库方法
export default useLoadingStore; 